<?php
# Sample script for RAWS tutorial 11: see http://rambla.eu/ for more info.
#
# Copyright (C) 2010 rambla.be

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
require_once '../RawsClient/Raws/Rass.php';

# Provide the path to a local testfile, that can be uploaded via RASS
define('LOCAL_FILE', '/my/path/to/a/file.mp4');

# Provide your own RASS credentials here
define('USER', 'xxx'); # your user account name
define('PWD', 'xxx'); # your user account pwd
define('CDN', "cdn0x"); # your sub-cdn (e.g. "cdn01")

define('BASE', 'http://rass.' . CDN . '.rambla.be/');

# all RASS API methods (except HEAD) will raise an exception if they don't return an HTTP SUCCESS CODE (200,201,204)
try {
  # Instantiate an object that manages the RASS connection, passing it your login credentials and the base service uri
  $rass = new Rass(USER, PWD, BASE);
  
  # Create a "tutorial11" directory under the root-directory
  $dir_entry = $rass->createDir("tutorial11", True);

  # Upload a file 'da_funk.mp4' to your dir and attach some metadata to it
  $item_entry = $rass->postItem($dir_entry->path, "da_funk.mp4", LOCAL_FILE);
  $meta_elems = array(new Rass_Extension_Meta("dc", "creator", "Rambla"), new Rass_Extension_Meta("dc", "description", "Behind the scenes at Ramblaworld"), new Rass_Extension_Meta("dc", "date", "2010-12-31"));
  $meta_entry = $rass->setMetadataFromPath($item_entry->path, array(), $meta_elems);
  
  # Upload a file 'stream.mp4' to your dir and attach some metadata to it
  $item_entry = $rass->postItem($dir_entry->path, "stream.mp4", LOCAL_FILE);
  $meta_elems = array(new Rass_Extension_Meta("dc", "creator", "Rambla"), new Rass_Extension_Meta("dc", "title", "Harder, Better, Faster, Stronger"), new Rass_Extension_Meta("dc", "description", "Guide to streaming with Wowza Media Server 02."), new Rass_Extension_Meta("dc", "date", "2009-07-09"));
  $meta_entry = $rass->setMetadataFromPath($item_entry->path, array(), $meta_elems);
  
  # Upload a file 'world.mp4' to your dir and attach some metadata to it
  $item_entry = $rass->postItem($dir_entry->path, "world.mp4", LOCAL_FILE);
  $meta_elems = array(new Rass_Extension_Meta("dc", "creator", "Verne Travels"), new Rass_Extension_Meta("dc", "title", "Around the World in Eighty Days"), new Rass_Extension_Meta("dc", "date", "2008-11-04") );
  $meta_entry = $rass->setMetadataFromPath($item_entry->path, array(), $meta_elems);

  # Upload a file 'clip.mp4' to your dir and attach some metadata to it
  $item_entry = $rass->postItem($dir_entry->path, "clip.mp4", LOCAL_FILE);
  $meta_elems = array(new Rass_Extension_Meta("dc", "creator", "Daft Punk"), new Rass_Extension_Meta("dc", "title", "Tron: Legacy"), new Rass_Extension_Meta("dc", "description", "Music video from the movie TRON legacy."), new Rass_Extension_Meta("dc", "date", "2010-12-06") );
  $meta_entry = $rass->setMetadataFromPath($item_entry->path, array(), $meta_elems);
  
  # Upload two test files, without any metadata
  $item_entry = $rass->postItem($dir_entry->path, "test1.mp4", LOCAL_FILE);
  $item_entry = $rass->postItem($dir_entry->path, "test2.mp4", LOCAL_FILE);
  
  
  # GET feed
  
  echo "Getting meta entries for files in directory: " . $dir_entry->path . "\n";
  $query = new Rass_ExtQuery("file");
  $feed = $rass->getExtFeed($dir_entry->path, $query);
  foreach ($feed as $entry) {
    echo "\n" . $entry->id->text;
  }

  # 'meta' searches
  
  echo "\n\nSearch for all files with a 'dc:title' element :";
  $query->setSearchArg("meta", array("dc:title"));
  $feed = $rass->getExtFeed($dir_entry->path, $query);
  foreach ($feed as $entry) {
    echo "\n found " . $entry->id->text;
  }
  
  echo "\n\nSearch for all files that have a 'dc:title' OR 'dc:description' element :";
  $query->setSearchArg("meta", array("dc:title", "dc:description"));
  $feed = $rass->getExtFeed($dir_entry->path, $query);
  foreach ($feed as $entry) {
    echo "\n found " . $entry->id->text;
  }
  
  echo "\n\nSearch for all files that have 'dc:creator' set to 'Daft Punk' :";
  $query->setSearchArg("meta", array("dc:creator:Daft Punk"));
  $feed = $rass->getExtFeed($dir_entry->path, $query);
  foreach ($feed as $entry) {
    echo "\n found " . $entry->id->text;
  }
  
  echo "\n\nSearch for all files that have 'dc:creator' set to 'Rambla' but don't have a title element :";
  $query->setSearchArg("meta", array("dc:creator:rambla"), array(), array("dc:title"));
  $feed = $rass->getExtFeed($dir_entry->path, $query);
  foreach ($feed as $entry) {
    echo "\n found " . $entry->id->text;
  }
  
  
  # 'qmeta' or 'iqmeta' searches on content
  
  # qmeta is case sensitive
  echo "\n\nCase-sensitive search for all files with meta elements that contain the string 'World' :";
  $query = new Rass_ExtQuery("file");
  $query->setSearchArg("qmeta", array("World"));
  $feed = $rass->getExtFeed($dir_entry->path, $query);
  foreach ($feed as $entry) {
    echo "\n found " . $entry->id->text;
  }
  
  # iqmeta is case insensitive
  echo "\n\nCase-insensitive search for all files with meta elements that contain the string 'world' :";
  $query = new Rass_ExtQuery("file");
  $query->setSearchArg("iqmeta", array("world"));
  $feed = $rass->getExtFeed($dir_entry->path, $query);
  foreach ($feed as $entry) {
    echo "\n found " . $entry->id->text;
  }
  
  # search operators can also be used with (i)qmeta
  echo "\n\nCase-insensitive search for all files with meta elements that contain the strings 'world' OR 'rambla' :";
  $query = new Rass_ExtQuery("file");
  $query->setSearchArg("iqmeta", array("world", "rambla"));
  $feed = $rass->getExtFeed($dir_entry->path, $query);
  foreach ($feed as $entry) {
    echo "\n found " . $entry->id->text;
  }
  
  # search operators can also be used with (i)qmeta
  echo "\n\nCase-insensitive search for all files with meta elements that contain the strings '2010' AND 'daft' :";
  $query = new Rass_ExtQuery("file");
  $query->setSearchArg("iqmeta", array(), array("2010", "Daft"), array());
  $feed = $rass->getExtFeed($dir_entry->path, $query);
  foreach ($feed as $entry) {
    echo "\n found " . $entry->id->text;
  }
  
  # combined searches are NOT ALLOWED => if set, the meta argument will prevail
  echo "\n\nSearch for all files with 'dc:creator'='Rambla' (the iqmeta argument will be ignored) :";
  $query = new Rass_ExtQuery("file");
  $query->setSearchArg("meta", array("dc:creator:Rambla"));
  $query->setSearchArg("iqmeta", array(), array(), array("2010"));
  $feed = $rass->getExtFeed($dir_entry->path, $query);
  foreach ($feed as $entry) {
    echo "\n found " . $entry->id->text;
  }
  
  # combined searches are NOT ALLOWED => you can unset a search argument
  echo "\n\nSearch for all files with with meta elements that don't contain the string '2010' or '2009 :";
  $query = new Rass_ExtQuery("file");
  $query->setSearchArg("meta", array("dc:creator:Rambla"));
  $query->unsetSearchArg("meta");
  $query->setSearchArg("iqmeta", array(), array(), array("2009","2010"));
  $feed = $rass->getExtFeed($dir_entry->path, $query);
  foreach ($feed as $entry) {
    echo "\n found " . $entry->id->text;
  }
  
  // # DELETE directory on the CDN. Set second argument to True to delete recursively.
  // $rass->deleteDir($dir_entry->path, True);
  // echo "\nDeleted dir: " . $dir_entry->path . "\n";
    


}
catch(Zend_Gdata_App_Exception $e) {
    # Report the exception to the user
    echo "\nCaught exception: " . get_class($e) . "\n";
    echo "Message: " . $e->getMessage() . "\n";
    # get the HTTP status code
    echo "HTTP Status Code: " . $e->getResponse()->getStatus() . "\n";
    echo "Response Body with exception details: " . $e->getResponse()->getBody() . "\n";
    # get the raws:error elements
    $rawsResponse = Raws::parseRawsResponse($e->getResponse());
    echo "Raws Code: " . $rawsResponse->getCode() . "\n";
    echo "Raws Message: " . $rawsResponse->getMsg() . "\n";
    $reasons = $rawsResponse->getReasons();
    foreach ($reasons as $reason) {
      echo "Raws Reason: " . $reason . "\n";
    }
    $details = $rawsResponse->getDetails();
    foreach ($details as $key => $value) {
      echo "Raws Detail: " . $key . " -> " . $value . "\n";
    }
}
catch (Zend_Exception $e) {
    echo "Caught exception: " . get_class($e) . "\n";
    echo "Message: " . $e->getMessage() . "\n";
}
